﻿using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LxCFD
{
    /// <summary>
    /// CoordinateSystem.xaml 的交互逻辑
    /// </summary>
    public partial class CoordinateSystem : UserControl
    {
        public CoordinateSystem()
        {
            InitializeComponent();

            Coordinate.Camera = _Camera = new OrthographicCamera()
            {
                Width = 70,
            };
            SetCamera(new Vector3D(0, 0, -1), new Vector3D(0, 1, 0));
            Coordinate.Children.Add(CreatCoordinateModelVisual());
        }
        private OrthographicCamera _Camera;
        public void SetCamera(Vector3D lookDir, Vector3D upDir)
        {
            upDir.Normalize();
            lookDir.Normalize();
            _Camera.UpDirection = upDir;
            _Camera.LookDirection = lookDir;
            _Camera.Position = (Point3D)(-60 * lookDir);

            Vector3D right = Vector3D.CrossProduct(lookDir, upDir);
            double ui3dRate = this.Width / _Camera.Width;
            MoveLabel(new Vector3D(30, 0, 0), LabelX);
            MoveLabel(new Vector3D(0, 30, 0), LabelY);
            MoveLabel(new Vector3D(0, 0, 30), LabelZ);
            void MoveLabel(in Vector3D axisV, Label tag)
            {
                double vup = Vector3D.DotProduct(axisV, upDir) * ui3dRate;
                double vright = Vector3D.DotProduct(axisV, right) * ui3dRate;
                Canvas.SetTop(tag, (this.Height - tag.Height) / 2 - vup);//canvas坐标左上为原点
                Canvas.SetLeft(tag, (this.Width - tag.Width) / 2 + vright);
            }
        }
        public ModelVisual3D CreatCoordinateModelVisual()
        {
            Model3DGroup modelGroup = new Model3DGroup();
            modelGroup.Children.Add(new AmbientLight(Colors.White));
            Geometry3DBuilder builder = new Geometry3DBuilder();
            double len = 6;
            double st = -len / 2;
            builder.AddHexahedron(new Point3D(st, st, st), new Vector3D(len, 0, 0), new Vector3D(0, len, 0), new Vector3D(0, 0, len));
            modelGroup.Children.Add(new GeometryModel3D()
            {
                Geometry = builder.MigrateToMesh(),
                Material = new DiffuseMaterial(Brushes.Black),
            });
            builder.AddArrow(new Point3D(), new Vector3D(25, 0, 0), 0.32, 1, 4, 8);
            Geometry3D axis = builder.MigrateToMesh();
            modelGroup.Children.Add(new GeometryModel3D()
            {
                Geometry = axis,
                Material = new DiffuseMaterial(Brushes.Red),
            });
            modelGroup.Children.Add(new GeometryModel3D()
            {
                Geometry = axis,
                Material = new DiffuseMaterial(Brushes.Green),
                Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), 90)),

            });
            modelGroup.Children.Add(new GeometryModel3D()
            {
                Geometry = axis,
                Material = new DiffuseMaterial(Brushes.Blue),
                Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), -90)),
            });
            return new ModelVisual3D() { Content = modelGroup };
        }

    }
}
